<?php
namespace Admin\Controller;
use Admin\Controller\CommonController;
class GoodsController extends CommonController {
	protected $table = 'product';//商品表
	protected $gc_class = 'goods_class';//商品分类表
	protected $st_class = 'store_class';//店铺分类表
	protected $tb_store = 'store';//店铺表

	/**
	*商品分类
	*/	
    public function index(){
		
		$num = 20 ;
		$order = 'gc_sort desc';
		$str = "gc_parent_id=%d ";
		$data = array(0);	
		//分页
		$show = Page($this->gc_class,$str,$data,$num);		
		$g_class = _getList($this->gc_class,$str,$data,'',$order,$_GET['p'],$num);
			
		$this->assign('g_class',$g_class) ;			

		if (I('get.gc_add') || I('get.id')) {
			
			if (I('get.id')) {
				//修改
				$where_1['gc_id'] = I('get.id');
				$gclist = _getOne($this->gc_class,$where_1);
				$type = 'gc_edit';
				$this->assign('gclist',$gclist) ;
			} else {
				//新增
				$type = 'gc_add';
			}
			
			$this->assign('type',$type) ;
			$this->display('gc_add');
		} else {
			$this->assign('page',$show) ;	
			$this->display();			
		}
    }
	/*
	*添加/修改 
	*商品分类
	*/
    public function gc_add(){
	
		if (IS_POST) {
			
			$type = I('post.type');
			
			switch ($type) {
				case 'gc_add':
					$url = 'index';
					I('post.gc_name') ? $data['gc_name'] = I('post.gc_name'):$this->error('分类名称不能为空！',$url);	
				    $data['gc_ioc'] = upload();
					I('post.commis_rate') ? $data['commis_rate'] = I('post.commis_rate'):$data['commis_rate'] = 0;
					
					$data['gc_parent_id'] = I('post.gc_parent_id') ;
					
					I('post.gc_sort') ? $data['gc_sort'] = I('post.gc_sort'):$data['gc_sort'] = 255;
					
					I('post.gc_show') && $data['gc_show'] = I('post.gc_show') ;
					
					$res = 1 ;
					$massege = '添加成功!' ;
					$error = '添加失败！' ;
					$table = $this->gc_class ;
					$where_1 ['gc_name'] = I('post.gc_name') ;
					$error2 = '分类已存在！' ;
				break;
				case 'gc_edit':
					$where ['gc_id'] = I('post.id');
					$url = 'index';
					I('post.gc_name') ? $data['gc_name'] = I('post.gc_name'):$this->error('分类名称不能为空！',$url);	
					
				    $data['gc_ioc'] = upload();
					
					I('post.commis_rate') && $data['commis_rate'] = I('post.commis_rate');

					I('post.gc_sort') && $data['gc_sort'] = I('post.gc_sort') ;
					I('post.gc_show') ? $data['gc_show'] = I('post.gc_show'):$data['gc_show'] = 0;
					$res = 2 ;
					$massege = '修改成功!' ;
					$error = '修改失败！' ;
					$table = $this->gc_class ;
					$where_1 ['gc_name'] = I('post.gc_name') ;
					$where_1 ['gc_id'] = array('neq',I('post.id')) ;
					$error2 = '分类已存在！' ;
				break;				
			}
			//dump(I('post.'));die;
			if ($where_1) {				
				$find_one = _getOne($table,$where_1);
				if ($find_one) {
					$this->error($error2,$url);
				}				
			}
			//dump($res);die;
			if ($res==1) {
				$change = _getAdd($table,$data);
				if ($change && $data['gc_parent_id']=='0') {
					$st_data ['sc_name'] = $data ['gc_name'] ;
					$st_data ['sc_bail'] = 0 ;
					$st_data ['sc_sort'] = 255 ;
					$add = _getAdd($this->st_class,$st_data);
				}
			} else if ($res==2) {
			
				$gcOne = _getOne($table,$where);
				$str2 = "sc_name='%s'" ;
				$data2 = array($gcOne['gc_name']);
				$data3['sc_name'] = $data['gc_name'] ;
				
				$change =  _getEdit($table,$where,'',$data);
				if ($change && $gcOne['gc_parent_id']=='0') {
					$Edit2 = _getEdit($this->st_class,$str2,$data2,$data3);	
				}
			}			
				
			unset($data);
			unset($res);
			unset($type);	
			unset($where);
			unset($table);
			unset($st_data);			
			if ($change) {
				//清楚缓存
				SClear() ;
				unset($change);
				$this->success($massege,$url);				
			} else {
				$this->error($error,$url);
			}			
		}
    }

	
	/**
	* 获取商品分类子级分类
	* 子级分类
	*/	
	public function gc_chlidren(){
			
		if (IS_POST) {
			unset($str) ;
			$data = array(I('post.id')); 
			$type = I('post.type');
			switch ($type) {
				case  'findson':				
					  $table = $this->gc_class;
					  $str = "gc_id=%d" ; 
					  $getOne = _getOne($table,$str,$data);	
					  if (!$getOne) {
							$this->ajaxReturn(array('info'=>0));	  
					  }
					   unset($str) ;
					   unset($order) ;
					   $str = "gc_parent_id=%d" ;
					   $order = 'gc_sort asc' ;
				break;
			}
				
			$list = _getList($table,$str,$data,'',$order);		
		
			unset($str);
			unset($data);		
			if ($list) {
				$this->ajaxReturn(array('info'=>$list));				
			} else {
				$this->ajaxReturn(array('info'=>0));	
			}			
		}		
	}	
	
	
	
	/**
	*商品列表
	*/	
	public function goods_list(){
		
		$join = array() ;
		if (!$_GET['p']) {
			$_GET['p'] = 1;
		}


		if ($_GET['op']=='illegal' || $_GET['clear']=='2') {
			$cache = '' ;
			$str = "productname<>'%s' and goods_state=%d";
			$data = array('',10);
			$html = 'goods_illegal' ;
		} else if ($_GET['select']=='s_goods') {
		//搜索
			if ($_GET ['goods_state']>0) {				
				$arr_data ['goods_state']  = $_GET ['goods_state']-1 ;
				$str1 = ' and goods_state=%d';
				$da_str1 = ','.$arr_data ['goods_state'] ;				
			} else {
				$str1 = '';
				$da_str1 = '' ;	
			}
			
			if ($_GET ['goods_verify']>0) {				
				$arr_data ['goods_verify']  = $_GET ['goods_verify'] - 1 ;
				$str2  = ' and goods_verify=%d';
				$da_str2 = ','.$arr_data ['goods_verify'] ;
			} else {
				$str2  = '';
				$da_str2 = '' ;
			}
			
			if ($_GET ['productname']) {								
				$arr_data ['productname']  = $_GET ['productname'] ;
				$str3  = " and productname LIKE '%s'";
				$da_str3 = ",%".htmlentities($arr_data['productname'])."%";
			} else {
				$str3  = '' ;
				$da_str3 = '' ;
			}
			
			if ($_GET ['number']) {								
				$arr_data ['number']  = $_GET ['number'] ;
				$str4  = ' and number="%s"';
				$da_str4 = ','.$_GET ['number'] ;
			} else {
				$str4  = '' ;
				$da_str4 = '' ;
			}
			
			if ($_GET ['st_name']) {								
				//店铺
				$st_list = _getOne($this->tb_store,array('store_name'=>$_GET ['st_name']));
				$str5  = ' and st_id="%s"';
				$da_str5 = ','.$st_list['store_id'] ;
			} else {
				$str5  = '' ;
				$da_str5 = '' ;
			}
			
			$da_str = "0".$da_str1.$da_str2.$da_str3.$da_str4.$da_str5 ;
			
			$str = "id>%d".$str1.$str2.$str3.$str4.$str5;
			
			$data = explode(',',$da_str);

		    //强制刷新
		   _Refresh(1,$cache);
		} else {
			$cache = 'goods_list'.$_GET['p'] ;
			$str = "productname<>'%s' and goods_state<>%d";
			$data = array('',10);
		}

/* 			dump($str);
			dump($data);die; */
		
		//强制刷新
		if ($_GET['clear']) {
			 _Refresh($_GET['clear'],$cache);
		}
		
		$num = 20 ;
		$order = 'id desc';		
		$join1 = ' LEFT  JOIN __STORE__ as st ON st.store_id=st_id ' ;
		//$join2 = 'LEFT JOIN __GOODS_CLASS__ as gc ON gc.gc_id=gc_id' ;
		$group ='' ;
		$field = '*,st.store_name,st.is_own_shop' ;
		
		//分页
		$show = Page($this->table,$str,$data,$num);		
		
		$list = _getJoinList($this->table,$join1,$str,$data,$cache,$order,$_GET['p'],$num,$field,$join2) ;

		//dump($list);die;
		$this->assign('html',$html) ;
		$this->assign('page',$show) ;
		$this->assign('list',$list) ;
		$this->display($html);
	}

	/**
	*违规下架列表
	*
	*/			
	public function goods_illegal() {
		
		$this->display();
	}	
	
	/**
	*修改商品状态 $type=1 商品表
	*修改商品状态
	*/	
	public function stata_edit(){
		
		if (IS_POST) {
						
			$str = I('post.str')."=%d" ; 
			$data = array(I('post.id')); 
			$type = I('post.table');
			switch ($type) {
				case '1':				
				$table = $this->table;
				$data2 ['goods_verify'] = I('post.state') ;
				if (I('post.state')=='1') {
					$msg = '审核成功，请强制刷新页面';
					$err = '审核失败';
				} else {
					$data2 ['goods_verify'] = 0 ;
					$data2 ['goods_state'] = I('post.state') ;
					$msg = '违规下架成功，请强制刷新页面';
					$err = '违规下架失败';
				}
				break;
			}
			
			$edit = _getEdit($table,$str,$data,$data2);				
			unset($str);
			unset($data);
			unset($data2);			
			if ($edit) {
				//清楚缓存
				SClear() ;
				$this->ajaxReturn(array('info'=>$msg));				
			} else {
				$this->ajaxReturn(array('info'=>$err));	
			}			
		}		
	}
	
	
	
	/**
	*删除商品列表 $type=1 商品表
	*
	*/	
	public function goods_del(){
		
		if (IS_POST) {
						
			$str = I('post.str')."=%d" ; 
			$data = array(I('post.id')); 
			$type = I('post.table');
			switch ($type) {
				case '1':				
				$table = $this->table;
				$msg = '删除成功';
				$err = '删除失败';
				break;
			}
			
			$del = _getDel($table,$str,$data);			
			unset($str);
			unset($data);		
			if ($del) {
				//清楚缓存
				SClear() ;
				$this->ajaxReturn(array('info'=>$msg));				
			} else {
				$this->ajaxReturn(array('info'=>$err));	
			}			
		}		
	}	
	
	
}